Vue.prototype.$ajax = axios;
axios.defaults.baseURL = "http://localhost:2333/taofuxn";

var app = new Vue({
    el: '#app',
    data: {
        tableData: [],
        loading: true,
        dialogVisible: false,
        labelPosition: 'right',
        formLabelAlign: {
            id: '',
            name: '',
            phone: '',
            address: '',
            note: '',
            level: ''
        },
        multipleSelection: [],
        isEdit: false,
        editData: null,
        rules: {
            date: [
                {required: true, message: '请选择日期', trigger: 'blur'},
            ],
            name: [
                {required: true, message: '请输入姓名', trigger: 'blur'},
                {min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur'}
            ],
            address: [
                {required: true, message: '请输入地址', trigger: 'blur'},
                {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'}
            ]
        }
    },
    created() {
        this.initData()

    },
    methods: {

        initData() {
            axios.get("/producer/getProducer?page=0&pageSize=100").then((response) => {
                if (response.data.code == 0) {
                    this.tableData = response.data.obj.infos;
                }else {
                    this.showTip('info',response.data.msg);
                }
                this.loading = false;
            }).catch((error) => {
                this.showTip('error', '查询数据失败');
            });

        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done()
                })
                .catch(_ => {
                })
        },
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        handleEdit(index, row) {
            this.isEdit = true
            this.editData = index
            this.formLabelAlign = JSON.parse(JSON.stringify(row))
            this.dialogVisible = true
        },


        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    /*处理编辑的http 请求逻辑*/
                    if (this.isEdit) {
                        this.submitByPost('/producer/modifyProducer', this.formLabelAlign,
                            (response) => {
                                this.showTip('success', '编辑成功了');
                                Object.assign(this.tableData[this.editData], this.formLabelAlign);
                                this.dialogVisible = false;
                            })

                        /* 处理添加的编辑逻辑*/
                    } else {
                        this.submitByPost('/producer/addProducer', this.formLabelAlign, (response) => {
                            this.showTip('success', '添加厂商成功');
                            this.tableData.unshift(this.formLabelAlign)
                            this.dialogVisible = false;
                        })
                    }

                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
        },

        submitByPost: function (url, params, callback) {
            axios.post(url, params).then((response) => {
                if (response.data.code === 0) {

                    /*结果码没问题了，将需要处理的数据回调出去*/
                    callback(response.data.obj);
                } else {

                    /*有问题的结果码则输出后台提示内容*/
                    this.showTip('info', response.data.msg);
                }
            }).catch((error) => {
                this.showTip('error', '接口请求错误了-_-!!');
            });
        },

        submitByGet: function (url, callback) {
            axios.get(url).then((response) => {
                if (response.data.code === 0) {

                    /*结果码没问题了，将需要处理的数据回调出去*/
                    callback(response.data.obj);
                } else {

                    /*有问题的结果码则输出后台提示内容*/
                    this.showTip('info', response.data.msg);
                }
            }).catch((error) => {
                this.showTip('error', '接口请求错误了-_-!!');
            });
        },


        showTip: function (type, message) {
            this.$message({
                message: message,
                type: type
            })
        },


        add() {
            this.isEdit = null
            this.dialogVisible = true
            this.formLabelAlign = {
                id: '',
                name: '',
                phone: '',
                address: '',
                note: '',
                level: ''
            }
        },

        del(type, index, row) {
            if (type == 'more') {
                if (!this.multipleSelection.length) {
                    this.showTip('error', '请选择需要删除的行');
                }
                return false
            }

            let curData = JSON.parse(JSON.stringify(row));

            console.log(this.multipleSelection)

            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                axios.get('/producer/deleteProducer?ProducerId=' + curData.id).then((response) => {
                    this.tableData.splice(index, 1);
                    this.showTip('success', '删除成功');
                }).catch((error) => {
                    this.showTip('error', '数据删除失败-_-!！');
                });

            }).catch(() => {
                this.showTip('info', '已取消删除-_-!！-_-!！');
            });
        }
    }
})

